package com.xdf.whiteaccount.service.impl;

import com.xdf.whiteaccount.entity.BillFabContract;
import com.xdf.whiteaccount.entity.FabContractDetail;
import com.xdf.whiteaccount.entity.FabContractMain;
import com.xdf.whiteaccount.service.BillCodeService;
import com.xdf.whiteaccount.service.BillFabContractService;
import com.xdf.whiteaccount.service.FabContractDetailService;
import com.xdf.whiteaccount.service.FabContractMainService;
import com.xdf.whiteaccount.utils.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BillFabContractServiceImpl implements BillFabContractService {
    @Lazy
    @Autowired
    private FabContractMainService mainService;
    @Lazy
    @Autowired
    private FabContractDetailService detailService;
    @Lazy
    @Autowired
    private BillFabContractServiceImpl meService;
    @Autowired
    private BillCodeService billCodeService;

    private static final String BILL_TYPE = "fab_contract";// 设备维保单据

    @Transactional
    @Override
    public int delete(Long id){
        List<FabContractDetail> detailList = detailService.selectByExample(new Example().andEq("parent_id", id));
        if(detailList != null && detailList.size() > 0){
            detailList.forEach(item -> detailService.deleteByPrimaryKey(item.getId()));
        }

        mainService.deleteByPrimaryKey(id);
        return 1;
    }

    /**
     * 主表没有主键则所有明细都为新增，主表有主键要检测每个明细数据
     * 有明细主键的则修改，没有的则新增
     * @param record
     * @return
     */
    @Transactional
    @Override
    public BillFabContract save(BillFabContract record) {
        Long idOrigin = Optional.ofNullable(record.getMain().getId()).orElse(Long.valueOf(0));
        record = meService.fullFill(record);

        List<FabContractDetail> detailList = record.getDetailList();
        Boolean hasDetail = detailList != null && detailList.size() > 0 ? true:false;

        if(idOrigin == 0){
            meService.doBeforeInsert(record);
            if(hasDetail){
                // 清除所有明细主键
                for (FabContractDetail item: detailList){
                    item.setId(null);
                }
            }
        }

        // 执行保存逻辑
        FabContractMain main = mainService.save(record.getMain());
        Long id = main.getId();
        if(hasDetail){
            for (FabContractDetail item: detailList){
                item.setParentId(id);
            }
            detailList = detailService.multiSave(detailList);
            record.setDetailList(detailList);
        }
        record.setMain(main);

        if(idOrigin == 0)
            meService.doAfterInsert(record);
        return record;
    }

    @Override
    public BillFabContract fullFill(BillFabContract record){
        String billCode = Optional.ofNullable(record.getMain().getBillCode()).orElse("");
        if(billCode.length() == 0){// 新增单据
            billCode = billCodeService.getNewBillCode(BILL_TYPE);
        }
        record.getMain().setBillCode(billCode);
        return record;
    }

    @Override
    public void doBeforeInsert(BillFabContract record) {
    }

    @Override
    public void doAfterInsert(BillFabContract record) {
    }

    @Override
    public BillFabContract selectByPrimaryKey(Long id){
        FabContractMain main = mainService.selectByPrimaryKey(id);
        List<FabContractDetail> detailList = detailService.selectByParentId(id);
        return BillFabContract.builder().main(main).detailList(detailList).build();
    }

    public List<FabContractDetail> getDetailList8PrimaryKey(List<Long> idList){
        return idList.stream().map(detailId -> detailService.selectByPrimaryKey(detailId)).collect(Collectors.toList());
    }

    @Override
    public BillFabContract moveBill(Long id, String moveBillFlag){
        FabContractMain FabContractMain = mainService.moveBill(id, moveBillFlag);
        if(FabContractMain == null) return null;
        List<FabContractDetail> detailList = detailService.selectByParentId(FabContractMain.getId());
        return BillFabContract.builder().main(FabContractMain).detailList(detailList).build();
    }
}
